/*
 * Copyright (C) 2012 deathowl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.console;

import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.List;
import rpg.RPG;
import rpg.gfx.Bitmap;
import rpg.gfx.OverlayEffect;

/**
 *
 * @author deathowl
 */
public class Console extends OverlayEffect {

    private int color = 0xcc000000;
    private boolean opened = false;
    private static Console instance;
    private String actualLine = "";
    private int linecnt = 0;
    private List<Bitmap> lines = new LinkedList<Bitmap>();

    public Console(int width, int height) {
        super(width, height, 0, 0);
        clearTop();
        clearBottom();
        linecnt = (height-30)/21;
        for (int i = 0; i < linecnt; i++) {
            lines.add(new Bitmap(1, 1));
        }
        printLine("This... is.. CONSOLE!!!",0xffffff00);
    }
    
    public void clearConsole(){
        lines.clear();
        for(int i = 0; i < linecnt; i++) {
            lines.add(new Bitmap(1,1));
        }
        clearTop();        
    }
    
    public void clearTop(){
        clearWith(color);
        for(int i = (height-30)*width+5;i<(height-29)*width-5;i++){
            pixels[i]= 0xffffffff;
        }
    }
    
    public void clearBottom(){
        for(int i = (height-29)*width;i<width*height;i++){
            pixels[i]= 0xcc000000;         
        }
        draw(RPG.fs.getText(actualLine, (byte) 2, 0xffffffff),6,height-25);
    }

    @Override
    public boolean tick() {
        if (opened) {

            if (RPG.keylistener.isKeyDown2(KeyEvent.VK_ENTER) && !actualLine.isEmpty()) {
                runLine(actualLine);
                RPG.keylistener.clearCache();
                actualLine = "";
            }
            
            if(RPG.keylistener.isKeyDown2(KeyEvent.VK_BACK_SPACE) && !actualLine.isEmpty()){
                actualLine = actualLine.substring(0, actualLine.length()-1);
                clearBottom();
            }
            
            String cache = RPG.keylistener.getAndClearCache();
            if(!cache.isEmpty()){
                actualLine += cache;
                clearBottom();
            }
        }
        return true;
    }

    public static void newInstance(int width, int height) {
        instance = new Console(width, height);
    }

    public static Console getInstance() {
        if (instance == null) {
            newInstance(RPG.WINDOW_WIDTH - 10, RPG.WINDOW_HEIGHT - 10);

        }
        return instance;
    }

    public boolean isVisible() {
        return opened;
    }

    public void setActualLine(String actualLine) {
        this.actualLine = actualLine;
    }

    public void open() {
        this.opened = true;
    }

    public void close() {
        this.opened = false;
    }

    public void toggle() {
        opened = !opened;
        actualLine="";
    }

    public void runLine(String line) {
        actualLine = "";
        printLine(line,0xffffffff);
        CommandParser cn = CommandParser.getInstance();
        cn.tryRun(line);
    }

    public void printLine(String line, int clr) {
        lines.add(RPG.fs.getText(line, (byte) 2, clr));
        if (lines.size() > linecnt) {
            lines.remove(0);
        }
        int i = 0;
        clearTop();
        clearBottom();
        for (Bitmap b : lines) {
            draw(b, 6, 6 + i * 21);
            i++;
        }
    }
}
